package com.qp;

import com.qp.opc.OpcUaClientFacade;
import com.qp.opc.OpcUaClientFactory;
import com.qp.opc.OpcUaConfig;
import com.qp.opc.OpcUaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角模块
 */
public class OpcJMK {
    //日志
    private static final Logger log = LoggerFactory.getLogger(OpcUaClientFactory.class);

    public static void main(String[] args) {
        try {
            //连接
            OpcUaConfig opcConfig = new OpcUaConfig().setUrl("opc.tcp://192.168.31.13:49320");
            OpcUaClientFacade client = OpcUaClientFactory.getClient(opcConfig);
            //订单数量
            int count = 2;
            int idx = 0;
            while (idx < count) {
                String key = "Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Ms-Zs_Data_20";
                Object flagV = OpcUaUtil.readValue(client, key);
                if ("2".equals(flagV.toString())) {
                    log.error("PLC没有执行完成，等待1.5s......");
                    Thread.sleep(1500);
                    continue;
                }
                log.error("下方第{}个订单", idx + 1);
                Map<String, Object> map = new HashMap<>();
                //***************************************************角膜块
                //左右总成
                map.put("Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Mes_Zs_AssType_L", "11A400035L-U3");
                map.put("Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Mes_Zs_AssType_R", "11A400035R-U3");
                //左右转向节
                map.put("Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Mes_Zs_SwlAssType_L", "1ED400053BD");
                map.put("Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Mes_Zs_SwlAssType_R", "1ED400054BD");
                //配方
                map.put("Jiao_Mokuai_PLC.Jiao_Mokuai_Product.Part_Type_51", "U3");
                //标识 mes写入2（下发完成），plc会变成1（处理完成），mes检查这个为1时可以下发下一个产品了
                map.put(key, (short) 2);
                //***************************************************角膜块
                log.info("********************写入开始");
                //赋值
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    OpcUaUtil.writeValue(client, entry.getKey(), entry.getValue());
                }
                log.info("********************写入结束");
//                log.info("********************读取开始");
//                //取值
                List<String> opcKeyList = new ArrayList<>(map.keySet());
                Map<String, Object> opcValueMap = OpcUaUtil.readValuesToMap(client, opcKeyList);
                if (opcValueMap != null) {
                    for (Map.Entry<String, Object> entry : opcValueMap.entrySet()) {
                        log.info("OPC 读取的Key: {}，值：{}", entry.getKey(), entry.getValue());
                    }
                }
//                log.info("********************读取结束");
                idx++;
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}